const fs = require("fs")
const path = require("path")
const utils = require("../Utils/utils")
const model = require("../DAO/model")
const express = require('express')
const formidable = require("formidable");
const md5 = require("md5")


var router = express.Router();
//var md5 = require("blueimp-md5")
var session = require('express-session')

//添加答案的接口
module.exports.frontAnswer = function(req, res) {

		var form = new formidable.IncomingForm();
		//保留袁术的拓展名
		form.keepExtensions = true
		form.parse(req, function(err, fields, files) {
			var Answer = {}
			Answer.user_id = req.session.userInfo.user_id
			Answer.desc = fields.desc
			Answer.code = fields.code
			Answer.comment_id = fields.comment_id
			Answer.createTime = utils.getTime()
			model.Answer.find({}, function(err, docs) {
				if(err) {
					res.send({
						rescode: "500",
						info: "服务器错误！"
					})
					return
				}
				Answer.id = docs.length + 1
				new_answer = new model.Answer(Answer)
				new_answer.save(function(err) {
					if(err) {
						res.send({
							rescode: "500",
							info: "服务器错误！"
						})
						return
					}
					res.send({
						rescode: "200",
						info: "问题提交成功！"
					})
					return
				})

			})
		})

}

//获取首页
module.exports.messageBord = function(req, res) {

	if(req.session.userInfo !== undefined && req.session.userInfo !== null) {
		model.User.findOne({
			email: req.session.userInfo.email
		}, function(err, doc) {
			if(!err) {
				req.session.userInfo.name = doc.name
				req.session.userInfo.avatar = doc.avatar
				res.render("./Guestbook/messageBord.html", {
					title: "留言板",
					loc: "messageBord",
					isLogin: req.session.userInfo.isLogin,
					name: req.session.userInfo.name,
					avatar: req.session.userInfo.avatar
				})
			} else {
				res.send("服务器错误！")
			}
		})

	} else {
		res.render("./Guestbook/messageBord.html", {
			title: "留言板",
			loc: "messageBord"
		})
	}

}

//  用户名   用户头像      
// 问题对应的答案
//

//获取所有的提问
module.exports.allComments = function(req, res) {
	model.Comment.find({}, function(err, docs) {
		var itemnum = docs.length
		var thecomments = []
		var jsonStr = JSON.stringify(docs);
		docs = JSON.parse(jsonStr);
		docs.forEach(function(eachComment, i) {

			model.User.findOne({
				id: eachComment.user_id
			}, function(err, doc) {
				eachComment.avatar = doc.avatar
				eachComment.name = doc.name

				model.Answer.find({
					comment_id: eachComment.id
				}, function(err, docs) {
					 eachComment["answers"] =docs
					thecomments.push(eachComment)

					if(thecomments.length == itemnum) {
						res.send(thecomments)
						return
					}
				})
			})
		})
	})
}

module.exports.getusersInfoByids = function(req, res) {
	var form = new formidable.IncomingForm();
	form.keepExtensions = true
	form.parse(req, function(err, fields, files) {
		var users_id = fields.users_id.split(",")
		var users = []
		users_id.forEach(function(item, i) {
			model.User.findOne({
				id: item
			}, function(err, doc) {
				var usersInfo = {}
				usersInfo.name = doc.name
				usersInfo.id = doc.id
				usersInfo.avatar = doc.avatar

				users[i] = usersInfo
				if(i == users_id.length - 1) {
					
					res.send({
						users: users
					})
					return
				}
			})
		})
	})
}

/*
 
 * commment  提问
 * id  
 * user_id
 * commentTitle  提问标题      commentFile  commentKnowledgeId  commentStepId
 * commentDesc   提问描述
 * commentImg    提问截图（）
 * commentKnowledge 问题的涉及标签
 * createTime
 * */

module.exports.frontComment = function(req, res) {

var form = new formidable.IncomingForm();
		form.uploadDir = "./static/img/commentImg" // 上传目录
		form.keepExtensions = true
		form.parse(req, function(err, fields, files) {
			var img_url_arr = []
			if(files !== undefined) {
			for( val in files ){
				var filepath  = files[val].path
				var commentImg_name = path.parse(filepath).base
				var img_url = path.join("/img/commentImg", commentImg_name)
				img_url_arr.push(img_url)
			}
		}
			
			

			var Comment = {}
			if(fields.commentTitle == "" || fields.commentdesc == "") {
				res.send({
					info: "调皮！"
				})
				return
			}
			Comment.title = fields.commentTitle
			Comment.desc = fields.commentdesc
			Comment.konwledgeTag = fields.commentKnowledgeId
			Comment.createTime = utils.getTime()
			Comment.img = img_url_arr
			Comment.user_id = req.session.userInfo.user_id
			model.Comment.find({}, function(err, docs) {
				if(err) {
					res.send({
						info: "服务器错误！"
					})
					return
				}
				Comment.id = docs.length + 1
				var new_comment = new model.Comment(Comment)
				new_comment.save(function(err) {
					if(err) {
						res.send({
							info: "服务器错误！"
						})
						return
					}
					res.send({
						info: "提问成功！"
					})
					return
				})

			})
		});
}